package com.wrs.examsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wrs.examsystem.entity.Answer;
import com.wrs.examsystem.entity.Result.R;
import com.wrs.examsystem.entity.Topic;
import com.wrs.examsystem.entity.bean.CorrectBean;
import com.wrs.examsystem.mapper.AnswerMapper;
import com.wrs.examsystem.mapper.TopicMapper;
import com.wrs.examsystem.service.AnswerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王荣升
 * @since 2024-05-15
 */
@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer> implements AnswerService {

    @Resource
    AnswerMapper answerMapper;

    @Resource
    TopicMapper topicMapper;


    @Override
    public R findGradeByStudentNumber(String studentNumber,Integer testId) {
//        查询该试卷的答案
        QueryWrapper<Topic> topicQueryWrapper = new QueryWrapper<>();
        topicQueryWrapper.eq("test_id",testId);
        List<Topic> topics = topicMapper.selectList(topicQueryWrapper);
//        查询学生的答案

        return null;
    }

    @Override
    public R updateGrandByStudentId(Integer topicId, Integer StudentId) {
        return null;
    }

    @Override
    public R correct(CorrectBean correctBean) {
        Integer correct = answerMapper.correct(correctBean.getTopicId(), correctBean.getStudentId(), correctBean.getAnswerMark());
        if (correct == 0){
            return R.error().message("批改失败");
        }
        return R.ok().message("批改成功");
    }

//    当前仅判断了单选和多选的情况（多选是错就不给分）
    @Override
    public R submitTest(List<Answer> answerList) {
//        获取试卷ID
        int testID = answerList.get(0).getTestId();
//        查询试卷答案集合
        QueryWrapper<Topic> topicQueryWrapper = new QueryWrapper<>();
        topicQueryWrapper.eq("test_id",testID);
        List<Topic> topics = topicMapper.selectList(topicQueryWrapper);
//        将正确答案存储在map集合中
        HashMap<Integer,String> hashMap = new HashMap<>();
        for (int i = 0; i < topics.size(); i++) {
            hashMap.put(topics.get(i).getTopicId(),topics.get(i).getTopicAnswer()+"|"+topics.get(i).getTopicMark());
        }
//        评分
        for (int i = 0; i < answerList.size(); i++) {
            String[] split = hashMap.get(answerList.get(i).getTopicId()).split("\\|");
            System.out.println("============split========");
            System.out.println(Arrays.toString(split));
            System.out.println(answerList.get(i).getAnswer());
            if (split[0].equals(answerList.get(i).getAnswer())){
                answerList.get(i).setAnswerMark(Integer.parseInt(split[1]));
            }else {
                answerList.get(i).setAnswerMark(0);
            }
            System.out.println("=====================================");
            System.out.println(Integer.getInteger(split[1]));
            System.out.println(answerList.get(i).toString());
            answerMapper.insert(answerList.get(i));
        }
        return R.ok();
    }

}
